//
//  StatusPictureView.swift
//  swiftImitationWeibo
//
//  Created by 杨果果 on 16/7/15.
//  Copyright © 2016年 yang. All rights reserved.
//

import UIKit

//  当前屏幕的宽度
let ScreenWidth = UIScreen.mainScreen().bounds.size.width
//  当前屏幕的高度
let ScreenHeight = UIScreen.mainScreen().bounds.size.height
//重用标记
private let PictureViewCellIdentifier = "PictureViewCellIdentifier"
//  每项的间距
private let itemMargin: CGFloat = 5
//  每项的宽度
private let itemWidth: CGFloat = (ScreenWidth - 2 * HomeCellMargin - 2 * itemMargin) / 3
class StatusPictureView: UICollectionView {
//创建一个
    var picUrls: [StatusPictureInfo]? {
        didSet {
            messageLabel.text = "\(picUrls?.count ?? 0)"
            let size = calcPictureViewSize(picUrls?.count ?? 0)
            self.snp_makeConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            self.reloadData()
        }
    }
    //懒加载控件
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.yellowColor(), fontSize: 30)
        return label
    }()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        flowLayout.minimumInteritemSpacing = itemMargin
        flowLayout.minimumLineSpacing = itemMargin
        super.init(frame: frame, collectionViewLayout: flowLayout)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI() {
        registerClass(StatusPictureViewCell.self, forCellWithReuseIdentifier: PictureViewCellIdentifier)
        dataSource = self
        addSubview(messageLabel)
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
    }
    private func calcPictureViewSize (count: Int) -> CGSize {
        let cols = count > 3 ? 3 : count
        let rows = (count - 1) / 3 + 1
        let width = CGFloat(cols) * itemWidth + CGFloat(cols - 1) * itemMargin
        let height = CGFloat(rows) * itemWidth + CGFloat(rows - 1) * itemMargin
        let size = CGSize(width: width, height: height)
        return size
    }
}
extension StatusPictureView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PictureViewCellIdentifier, forIndexPath: indexPath) as! StatusPictureViewCell
        cell.picInco = picUrls![indexPath.item]
        return cell
    }
}
class StatusPictureViewCell: UICollectionViewCell {
    var picInco: StatusPictureInfo? {
        didSet {
            if let imageUrl = picInco?.thumbnail_pic {
                imageView.sd_setImageWithURL(NSURL(string: imageUrl), placeholderImage: UIImage(named: "timeline_image_placeholder"))
                //判断图片地址是否以。gif结尾
                gifImageView.hidden = !imageUrl.hasPrefix(".gif")
            }
        }
    }
    private lazy var imageView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        view.contentMode = UIViewContentMode.ScaleAspectFill
        view.clipsToBounds = true
        return view
    }()
    
    private lazy var gifImageView : UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI() {
        contentView.addSubview(imageView)
        contentView.addSubview(gifImageView)
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(imageView)
            make.bottom.equalTo(imageView)
        }
        
    }
    
}